package com.jf.es.sql.restapi.restapi;

import lombok.extern.slf4j.Slf4j;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("es/sql/rest/api")
public class RestApiController {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping()
    public ResponseEntity<Object> test(String sql) {
        String url = "https://180.76.150.245:9200/yf_test_index_01?pretty";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String username = "elastic";
        String password = "elastic_123";
        String auth = username + ":" + password;
        byte[] encodedAuth = Base64Utils.encode(auth.getBytes());
        String authHeaderValue = "Basic " + new String(encodedAuth);
        headers.set("Authorization", authHeaderValue);



        Map<String, String> params = new HashMap<>();

        HttpEntity<String> httpEntity = new HttpEntity<>(headers);

        final ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, String.class, params);

        log.info(" result: {}", response);
        return ResponseEntity.ok(response);
    }
}
